<?php

namespace services\statistics;

use Yii;
use common\models\clinic\Clinic;
use common\models\patient\RevisitingRecord;
use common\components\Service;

/**
 * Class MonthNumReportService
 * @package services\statistics
 */
class MonthNumReportService extends Service
{
    /**
     * 科室病人月就诊次数人数汇总表
     *
     * @param $projectId
     * @param $monthIs
     * @return array|mixed|\yii\db\ActiveRecord[]
     */
    public function getMonthNum($projectId, $monthIs)
    {
        // 月末时间戳
        $monthNext =  strtotime(date('Y-m-d', $monthIs) . "+1 month") - 1;

        // 诊室列表
        $clinic = Clinic::find()
            ->select('id, name, sort')
            ->where([
                'project_id' => $projectId,
                'status' => Clinic::STATUS_ENABLE
            ])
            ->orderBy(['sort' => SORT_ASC, 'created_at' => SORT_DESC])
            ->asArray()
            ->all();

        // 来院记录
        $list = RevisitingRecord::find()
            ->alias('a')
            ->select('a.id, a.patient_card_id, a.clinic_id')
            ->leftJoin(Clinic::tableName(). ' b', 'a.clinic_id = b.id')
            ->where([
                'and',
                [
                    'a.is_sum' => RevisitingRecord::IS_SUM_YES,
                    'a.project_id' => $projectId,
                    'a.type' => [RevisitingRecord::TYPE_CONSUME, RevisitingRecord::TYPE_TREAT, RevisitingRecord::TYPE_IN_HOSPITAL],
                    'b.status' => Clinic::STATUS_ENABLE
                ],
                ['between', 'a.day_time', $monthIs, $monthNext],
            ])
            ->asArray()
            ->all();

        // 诊室整理
        $clinicList = [];
        foreach ($clinic as $cl) {
            $clinicList[$cl['id']] = [];
            // $clinicList[$cl['id']]['name'] = $cl['name'];
        }

        // 患者来院次数
        foreach ($list as $li) {
            if (isset($clinicList[$li['clinic_id']]) && isset($clinicList[$li['clinic_id']][$li['patient_card_id']])) {
                $clinicList[$li['clinic_id']][$li['patient_card_id']] += 1;
            } else {
                $clinicList[$li['clinic_id']][$li['patient_card_id']] = 1;
            }
        }

        $clinicList = array_values($clinicList);
        // 来院数据整理
        foreach ($clinicList as $key => &$cll) {
            $clinicList[$key] = array_count_values($cll);
        }

        // 诊室统计数据整理
        $list = $this->numList($clinicList);

        // 将诊室名称加入数组
        $clinincName = array_column($clinic, 'name');
        foreach ($list as $f => &$final) {
            $final['name'] = $clinincName[$f];
        }

        return $list;
    }

    /**
     * 诊室统计数据整理
     *
     * @param $clinicList
     * @return mixed
     */
    public function numList($clinicList)
    {
        foreach ($clinicList as $key => &$val) {

            $val['num_15+'] = 0;

            // 15+次数
            foreach ($val as $k => $c) {
                if ($k >= 15) {
                    $val['num_15+'] += $c;
                }
            }

            if ($val['num_15+'] == 0) {
                $val['num_15+'] = '/';
            }

            // 1-15次数
            for ($i = 1; $i <= 15; $i++) {
                if (!isset($val[$i])) {
                    $val['num_'.$i] = '/';
                } else {
                    $val['num_'.$i] = $val[$i];
                }
                unset($val[$i]);
            }

        }
        return $clinicList;
    }

}
